package controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.*;
import javax.servlet.http.*;

import java.util.List;
import java.io.*;
import java.nio.charset.StandardCharsets;

import service.guide_service;
import entity.*;

public class show_guide_servlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	private guide_service gd_service=new guide_service();
	public show_guide_servlet() {
		super();
	}
	public void destroy() {
		super.destroy(); 
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		request.setCharacterEncoding("utf-8");
		HttpSession session= request.getSession();
		String requestURI = request.getRequestURI();
		user u=(user)session.getAttribute("user");
		String userAgent = request.getHeader("User-Agent");
		boolean is_mobile = false;
		String[] mobileKeywords = {"Android", "iPhone", "iPad", "Windows Phone"};
		for (String keyword : mobileKeywords) {
		    if (userAgent.contains(keyword)) {
		    	is_mobile = true;
		        break;
		    }
		}
		request.setAttribute("is_mobile", is_mobile);
		if (requestURI.equals("/teamfight_wiki/hero_guide")){
			List<hero>heros=gd_service.get_all_hero("");
			request.setAttribute("is_edit",false);
			if (u!=null && u.getType()==2){
				request.setAttribute("is_edit",true);
			}
			request.setAttribute("heros", heros);
			request.getRequestDispatcher("/guide/hero_guide.jsp").forward(request,response);
		}
		else if (requestURI.equals("/teamfight_wiki/trammel_guide")){
			List<trammel>trammels=gd_service.get_all_trammel("");
			request.setAttribute("is_edit",false);
			if (u!=null && u.getType()==2){
				request.setAttribute("is_edit",true);
			}
			request.setAttribute("trammels", trammels);
			request.getRequestDispatcher("/guide/trammel_guide.jsp").forward(request,response);
		}
		else if (requestURI.equals("/teamfight_wiki/hex_guide")){
			List<hex>hexs=gd_service.get_all_hex("");
			request.setAttribute("is_edit",false);
			if (u!=null && u.getType()==2){
				request.setAttribute("is_edit",true);
			}
			request.setAttribute("hexs", hexs);
			request.getRequestDispatcher("/guide/hex_guide.jsp").forward(request,response);
		}
		else if (requestURI.equals("/teamfight_wiki/equip_guide")){
			List<equip>equips=gd_service.get_all_equip("");
			request.setAttribute("is_edit",false);
			if (u!=null && u.getType()==2){
				request.setAttribute("is_edit",true);
			}
			request.setAttribute("equips", equips);
			request.getRequestDispatcher("/guide/equip_guide.jsp").forward(request,response);
		}
		
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
}
